package com.example.onlinereadingsystem.demos.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.onlinereadingsystem.demos.web.mapper.UserBookShelfMapper;
import com.example.onlinereadingsystem.demos.web.dto.domain.Bookshelf;
import com.example.onlinereadingsystem.demos.web.dto.domain.Novel;
import com.example.onlinereadingsystem.demos.web.dto.vo.UserBookShelfListVo;
import com.example.onlinereadingsystem.demos.web.service.BookshelfService;
import com.example.onlinereadingsystem.demos.web.service.NovelService;
import com.example.onlinereadingsystem.demos.web.service.UserBookShelfService;
import com.example.onlinereadingsystem.demos.web.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 用户书架模块
 * @Author ljm
 * @Date 2024/11/6
 */
@Service
public class UserBookShelfServiceImpl implements UserBookShelfService {

    @Autowired
    private BookshelfService bookShelfService;
    @Autowired
    private UserBookShelfMapper userBookShelfMapper;

    @Autowired
    private NovelService novelService;

    /**
     * @auther ljm
     * @create 2024/11/6
     * @describe 根据用户id获取书架列表
     */
    @Override
    public Result<List<UserBookShelfListVo>> list(String id) {
        LambdaQueryWrapper<Bookshelf> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Bookshelf::getUserId, id);
        List<Bookshelf> list = bookShelfService.list(queryWrapper);
        List<UserBookShelfListVo> resList = new ArrayList<>();
        list.forEach(item -> {
            UserBookShelfListVo userBookShelfListVo = new UserBookShelfListVo();
            LambdaQueryWrapper<Novel> novelQueryWrapper = new LambdaQueryWrapper<>();
            novelQueryWrapper.eq(Novel::getId, item.getNovelId());
            Novel novel = novelService.getOne(novelQueryWrapper);
            BeanUtils.copyProperties(novel, userBookShelfListVo);
            userBookShelfListVo.setCurrentChapter(item.getCurrentChapter());
            userBookShelfListVo.setCurrentChapterId(item.getCurrentChapterId());
            resList.add(userBookShelfListVo);
        });
        return Result.OK(resList);
    }
}
